{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Iterative Least Squares for Sensor Fusion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "        <style>\n",
       "        .output_wrapper, .output {\n",
       "            height:auto !important;\n",
       "            max-height:100000px; \n",
       "        }\n",
       "        .output_scroll {\n",
       "            box-shadow:none !important;\n",
       "            webkit-box-shadow:none !important;\n",
       "        }\n",
       "        </style>\n",
       "    "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#format the book\n",
    "from __future__ import division, print_function\n",
    "%matplotlib inline\n",
    "import sys\n",
    "sys.path.insert(0, '..');\n",
    "sys.path.insert(0, '../kf_book')\n",
    "import book_format\n",
    "book_format.set_style()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A broad category of use for the Kalman filter is *sensor fusion*. For example, we might have a position sensor and a velocity sensor, and we want to combine the data from both to find an optimal estimate of state. In this section we will discuss a different case, where we have multiple sensors providing the same type of measurement. \n",
    "\n",
    " The Global Positioning System (GPS) is designed so that at least 6 satellites are in view at any time at any point on the globe. The GPS receiver knows the location of the satellites in the sky relative to the Earth. At each epoch (instant in time) the receiver gets a signal from each satellite from which it can derive the *pseudorange* to the satellite. In more detail, the GPS receiver gets a signal identifying the satellite along with the time stamp of when the signal was transmitted. The GPS satellite has an atomic clock on board so this time stamp is extremely accurate. The signal travels at the speed of light, which is constant in a vacuum, so in theory the GPS should be able to produce an extremely accurate distance measurement to the measurement by measuring how long the signal took to reach the receiver. There are several problems with that. First, the signal is not traveling through a vacuum, but through the atmosphere. The atmosphere causes the signal to bend, so it is not traveling in a straight line. This causes the signal to take longer to reach the receiver than theory suggests. Second, the on board clock on the GPS *receiver* is not very accurate, so deriving an exact time duration is nontrivial. Third, in many environments the signal can bounce off of buildings, trees, and other objects, causing either a longer path or *multipaths*, in which case the receive receives both the original signal from space and the reflected signals. \n",
    "\n",
    "Let's look at this graphically. I will do this in 2D to make it easier to graph and see, but of course this will generalize to three dimensions. We know the position of each satellite and the range to each (the range is called the *pseudorange*; we will discuss why later). We cannot measure the range exactly, so there is noise associated with the measurement, which I have depicted with the thickness of the lines. Here is an example of four pseudorange readings from four satellites. I positioned them in a configuration which is unlikely for the actual GPS constellation merely to make the intersections easy to visualize. Also, the amount of error shown is not to scale with the distances, again to make it easier to see."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAIABJREFUeJzs3Xd4VceZP/DvnHObdNULaqiBGqIJEGDAYDq4YYONjbEdO7HjOHY2idM22SS/ZJPNbnaTbMrGcYsxjm1cAgaDwXQwxTQJkGgSqHfUpdvr/P4QYAHSvfece85tms/z6AGkM3NeVF7NnTPzDqGUgmEYhgl9nL8DYBiGYXyDJXyGYZgRgiV8hmGYEYIlfIZhmBGCJXyGYZgRgiV8hmGYEYIlfIZhmBGCJXyGYZgRgiV8hmGYEULh7wAGS0hIoFlZWf4Og2EYJqiUlpZ2UkoT3V0XUAk/KysLJSUl/g6DYRgmqBBC6j25jk3pMAzDjBABNcJnGF+yWM3o1fXAYNTD7nDA4XSAEAKe46FQKBCljUZ0ZAwUPPsxYUID+05mQprT6URrZwvqmmvQ0FKL5vYm9PR3o0/XC7PF5FEfEeGRiImKRVx0PNKTM5GVNgaZqdmIjYoDIUTm/wHDSIclfCakUErRfLURZytKcf5KGRpa62G1WbzqU2/UQW/UoamtAeWVZ268P1IbhTG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p0bW3530clytDxnxLvaLlAP5GCAnUYi9/HPS53eHvYG517fP2MoC7ARQCeOza5zcYLLj2eQ3k5YLrMfA9OtiPAeyjlOYC2Hft34FiPW6PFwjw72NPjKiETym9RCmt9HccQriI+Ua9IkppLYDr9YoY4WYAqKKU1lBKrQA+wMDnl5EApfQQgO5b3v0AgLev/f1tAA/6NCgXhok3JIyohO9GNiHkDCHkc0LIXH8H44Fgqlf0LUJI+bWXygHz0n2QYPpcDkYB7CaElF4rURJMkiilrQBw7c9gOMk90L+P3Qq5hE8I2UsIOT/Em6sRWyuADErpFADfw8Au4SjfRCw6Zo/rFcnNTfyvABgLoAgDn+c/+CNGNwLmcynQHErpVAxMRb1ICJnn74BCWDB8H7sVcideian9c62Ms+Xa30sJIdUA8gD45EGY3PWK5OZp/ISQNwB8KnM4YgTM51IISmnLtT/bCSGbMTA1NdTzqUB0lRCSQiltJYSkAGj3d0CuUEpvnJ8ZwN/HboXcCF8MQkji9Qee/7+9O1SpIIjCOP7/UDSITTFfwe4TyE2abxBsNxg0+A6CSax2o4JF2KTPYLFYRXyOhWOY3SC6997kjM73SxMPy+xhmcN+I2mblP3zlrequRrgSNKqpBEL5hX9tu5l7k1IQ+jSPAM7kkaSVkjD8CZzTTNJWpO03q+Bfcp8tkMaYNqtp8BQ8m4R/sg+nuvffeHPImkCXAObpOyfl4g4APaAC0kt6UrG04goYmgzVHNEvErq84pays0rupK0SzoieQdO8pbzXUS0ks6AJ2AJuOnyoEq2BTwoXT60DNxGxGPekn4m6Q4YAxtd9tY5cAncSzoGPoDDfBV+NVDvuPR9vAj/aWtmVgkf6ZiZVcIN38ysEm74ZmaVcMM3M6uEG76ZWSXc8M3MKuGGb2ZWCTd8M7NKfAI4FI3dgt74qQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import ukf_internal\n",
    "ukf_internal.show_four_gps()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In 2D two measurements are sometimes enough to determine a unique solution. There are two intersections of the range circles, but often the second intersection is not physically realizable (it is in space, or under ground). However, with GPS we also need to solve for time, so we would need a third measurement to get a 2D position.\n",
    "\n",
    "However, since GPS is a 3D system we need to solve for the 3 dimensions of space, and 1 dimension of time. That is 4 unknowns, so in theory with 4 satellites we have all the information we need. However, we normally have at least 6 satellites in view, and often more than 6. This means the system is *overdetermined*. Finally, because of the noise in the measurements none of pseudoranges intersect exactly.\n",
    "\n",
    "If you are well versed in linear algebra you know that this an extremely common problem in scientific computing, and that there are various techniques for solving overdetermined systems. Probably the most common approach used by GPS receivers to find the position is the *iterative least squares* algorithm, commonly abbreviated ILS. As you know, if the errors are Gaussian then the least squares algorithm finds the optimal solution. In other words, we want to minimize the square of the residuals for an overdetermined system.\n",
    "\n",
    "Let's start with some definitions which should be familiar to you. First, we define the innovation as \n",
    "\n",
    "$$\\delta \\mathbf{\\bar{z}}= \\mathbf z - h(\\mathbf{\\bar{x}})$$\n",
    "\n",
    "where $\\mathbf z$ is the measurement, $h(\\bullet)$ is the measurement function, and $\\delta \\mathbf{\\bar{z}}$ is the innovation, which we abbreviate as $y$ in FilterPy. In other words, this is the equation $\\mathbf{y} = \\mathbf z - \\mathbf{H\\bar{x}}$ in the linear Kalman filter's update step.\n",
    "\n",
    "Next, the *measurement residual* is\n",
    "\n",
    "$$\\delta \\mathbf z^+ = \\mathbf z - h(\\mathbf x^+)$$\n",
    "\n",
    "I don't use the plus superscript much because I find it quickly makes the equations unreadable, but $\\mathbf x^+$ is the *a posteriori* state estimate, which is the predicted or unknown future state. In other words, the predict step of the linear Kalman filter computes this value. Here it is stands for the value of x which the ILS algorithm will compute on each iteration.\n",
    "\n",
    "These equations give us the following linear algebra equation:\n",
    "\n",
    "$$\\delta \\mathbf z^- = \\mathbf H\\delta \\mathbf x + \\delta \\mathbf z^+$$\n",
    "\n",
    "$\\mathbf H$ is our measurement function, defined as\n",
    "\n",
    "$$\\mathbf H = \\frac{d\\mathbf H}{d\\mathbf x} = \\frac{d\\mathbf z}{d\\mathbf x}$$\n",
    "\n",
    "We find the minimum of an equation by taking the derivative and setting it to zero. In this case we want to minimize the square of the residuals, so our equation is"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$ \\frac{\\partial}{\\partial \\mathbf x}({\\delta \\mathbf z^+}^\\mathsf{T}\\delta \\mathbf z^+) = 0,$$\n",
    "\n",
    "where\n",
    "\n",
    "$$\\delta \\mathbf z^+=\\delta \\mathbf z^- - \\mathbf H\\delta \\mathbf x.$$\n",
    "\n",
    "Here I have switched to using the matrix $\\mathbf H$ as the measurement function. We want to use linear algebra to peform the ILS, so for each step we will have to compute the matrix $\\mathbf H$ which corresponds to $h(\\mathbf{x^-})$ during each iteration.  $h(\\bullet)$ is usually nonlinear for these types of problems so you will have to linearize it at each step (more about this soon).\n",
    "\n",
    "For various reasons you may want to weigh some measurement more than others. For example, the geometry of the problem might favor orthogonal measurements, or some measurements may be more noisy than others. We can do that with the equation\n",
    "\n",
    "$$ \\frac{\\partial}{\\partial \\mathbf x}({\\delta \\mathbf z^+}^\\mathsf{T}\\mathbf{W}\\delta \\mathbf z^+) = 0$$\n",
    "\n",
    "If we solve the first equation for ${\\delta \\mathbf x}$ (the derivation is shown in the next section) we get\n",
    "\n",
    "$${\\delta \\mathbf x} = {{(\\mathbf H^\\mathsf{T}\\mathbf H)^{-1}}\\mathbf H^\\mathsf{T} \\delta \\mathbf z^-}\n",
    "$$\n",
    "\n",
    "And the second equation yields\n",
    "\n",
    "$${\\delta \\mathbf x} = {{(\\mathbf H^\\mathsf{T}\\mathbf{WH})^{-1}}\\mathbf H^\\mathsf{T}\\mathbf{W} \\delta \\mathbf z^-}\n",
    "$$\n",
    "\n",
    "Since the equations are overdetermined we cannot solve these equations exactly so we use an iterative approach. An initial guess for the position is made, and this guess is used to compute  for $\\delta \\mathbf x$ via the equation above. $\\delta \\mathbf x$ is added to the intial guess, and this new state is fed back into the equation to produce another $\\delta \\mathbf x$. We iterate in this manner until the difference in the measurement residuals is suitably small."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Derivation of ILS Equations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I will implement the ILS in code, but first let's derive the equation for $\\delta \\mathbf x$. You can skip the derivation if you want, but it is somewhat instructive and not too hard if you know basic linear algebra and partial differential equations.\n",
    "\n",
    "Substituting $\\delta \\mathbf z^+=\\delta \\mathbf z^- - \\mathbf H\\delta \\mathbf x$ into the partial differential equation we get\n",
    "\n",
    "$$ \\frac{\\partial}{\\partial \\mathbf x}(\\delta \\mathbf z^- -\\mathbf H \\delta \\mathbf x)^\\mathsf{T}(\\delta \\mathbf z^- - \\mathbf H \\delta \\mathbf x)=0$$\n",
    "\n",
    "which expands to\n",
    "\n",
    "$$ \\frac{\\partial}{\\partial \\mathbf x}({\\delta \\mathbf x}^\\mathsf{T}\\mathbf H^\\mathsf{T}\\mathbf H\\delta \\mathbf x - \n",
    "{\\delta \\mathbf x}^\\mathsf{T}\\mathbf H^\\mathsf{T}\\delta \\mathbf z^- - \n",
    "{\\delta \\mathbf z^-}^\\mathsf{T}\\mathbf H\\delta \\mathbf x +\n",
    "{\\delta \\mathbf z^-}^\\mathsf{T}\\delta \\mathbf z^-)=0$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We know that \n",
    "\n",
    "$$\\frac{\\partial \\mathbf{A}^\\mathsf{T}\\mathbf B}{\\partial \\mathbf B} = \\frac{\\partial \\mathbf B^\\mathsf{T}\\mathbf{A}}{\\partial \\mathbf B} = \\mathbf{A}^\\mathsf{T}$$\n",
    "\n",
    "Therefore the third term can be computed as\n",
    "\n",
    "$$\\frac{\\partial}{\\partial \\mathbf x}{\\delta \\mathbf z^-}^\\mathsf{T}\\mathbf H\\delta \\mathbf x = {\\delta \\mathbf z^-}^\\mathsf{T}\\mathbf H$$\n",
    "\n",
    "and the second term as\n",
    "\n",
    "$$\\frac{\\partial}{\\partial \\mathbf x}{\\delta \\mathbf x}^\\mathsf{T}\\mathbf H^\\mathsf{T}\\delta \\mathbf z^-={\\delta \\mathbf z^-}^\\mathsf{T}\\mathbf H$$\n",
    "\n",
    "We also know that\n",
    "$$\\frac{\\partial \\mathbf B^\\mathsf{T}\\mathbf{AB}}{\\partial \\mathbf B} = \\mathbf B^\\mathsf{T}(\\mathbf{A} + \\mathbf{A}^\\mathsf{T})$$\n",
    "\n",
    "Therefore the first term becomes\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "\\frac{\\partial}{\\partial \\mathbf x}{\\delta \\mathbf x}^\\mathsf{T}\\mathbf H^\\mathsf{T}\\mathbf H\\delta \\mathbf x &= {\\delta \\mathbf x}^\\mathsf{T}(\\mathbf H^\\mathsf{T}\\mathbf H + {\\mathbf H^\\mathsf{T}\\mathbf H}^\\mathsf{T})\\\\\n",
    "&= {\\delta \\mathbf x}^\\mathsf{T}(\\mathbf H^\\mathsf{T}\\mathbf H + \\mathbf H^\\mathsf{T}\\mathbf H) \\\\\n",
    "&= 2{\\delta \\mathbf x}^\\mathsf{T}\\mathbf H^\\mathsf{T}\\mathbf H\n",
    "\\end{aligned}$$\n",
    "\n",
    "Finally, the fourth term is\n",
    "\n",
    "$$ \\frac{\\partial}{\\partial \\mathbf x}\n",
    "{\\delta \\mathbf z^-}^\\mathsf{T}\\delta \\mathbf z^-=0$$\n",
    "\n",
    "Replacing the terms in the expanded partial differential equation gives us\n",
    "\n",
    "$$\n",
    " 2{\\delta \\mathbf x}^\\mathsf{T}\\mathbf H^\\mathsf{T}\\mathbf H -\n",
    " {\\delta \\mathbf z^-}^\\mathsf{T}\\mathbf H - {\\delta \\mathbf z^-}^\\mathsf{T}\\mathbf H\n",
    " =0\n",
    "$$\n",
    "\n",
    "$${\\delta \\mathbf x}^\\mathsf{T}\\mathbf H^\\mathsf{T}\\mathbf H -\n",
    " {\\delta \\mathbf z^-}^\\mathsf{T}\\mathbf H = 0$$\n",
    " \n",
    "$${\\delta \\mathbf x}^\\mathsf{T}\\mathbf H^\\mathsf{T}\\mathbf H =\n",
    " {\\delta \\mathbf z^-}^\\mathsf{T}\\mathbf H$$\n",
    "\n",
    "Multiplying each side by $(\\mathbf H^\\mathsf{T}\\mathbf H)^{-1}$ yields\n",
    "\n",
    "$${\\delta \\mathbf x}^\\mathsf{T} =\n",
    "{\\delta \\mathbf z^-}^\\mathsf{T}\\mathbf H(\\mathbf H^\\mathsf{T}\\mathbf H)^{-1}$$\n",
    "\n",
    "Taking the transpose of each side gives\n",
    "\n",
    "$$\\begin{aligned}\n",
    "{\\delta \\mathbf x} &= ({{\\delta \\mathbf z^-}^\\mathsf{T}\\mathbf H(\\mathbf H^\\mathsf{T}\\mathbf H)^{-1}})^\\mathsf{T} \\\\\n",
    "&={{(\\mathbf H^\\mathsf{T}\\mathbf H)^{-1}}^T\\mathbf H^\\mathsf{T} \\delta \\mathbf z^-} \\\\\n",
    "&={{(\\mathbf H^\\mathsf{T}\\mathbf H)^{-1}}\\mathbf H^\\mathsf{T} \\delta \\mathbf z^-}\n",
    "\\end{aligned}$$\n",
    "\n",
    "For various reasons you may want to weigh some measurement more than others. We can do that with the equation\n",
    "\n",
    "$$ \\frac{\\partial}{\\partial \\mathbf x}({\\delta \\mathbf z}^\\mathsf{T}\\mathbf{W}\\delta \\mathbf z) = 0$$\n",
    "\n",
    "Replicating the math above with the added $\\mathbf{W}$ term results in\n",
    "\n",
    "$${\\delta \\mathbf x} = {{(\\mathbf H^\\mathsf{T}\\mathbf{WH})^{-1}}\\mathbf H^\\mathsf{T}\\mathbf{W} \\delta \\mathbf z^-}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Implementing Iterative Least Squares"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our goal is to implement an iterative solution to \n",
    "$${\\delta \\mathbf x} = {{(\\mathbf H^\\mathsf{T}\\mathbf H)^{-1}}\\mathbf H^\\mathsf{T} \\delta \\mathbf z^-}\n",
    "$$\n",
    "\n",
    "First, we have to compute $\\mathbf H$, where $\\mathbf H =  d\\mathbf z/d\\mathbf x$. To keep the example small so the results are easier to interpret we will do this in 2D. Therefore for $n$ satellites $\\mathbf H$ expands to\n",
    "\n",
    "$$\\mathbf H = \\begin{bmatrix}\n",
    "\\frac{\\partial p_1}{\\partial x_1} & \\frac{\\partial p_1}{\\partial y_1} \\\\\n",
    "\\frac{\\partial p_2}{\\partial x_2} & \\frac{\\partial p_2}{\\partial y_2} \\\\\n",
    "\\vdots & \\vdots \\\\\n",
    "\\frac{\\partial p_n}{\\partial x_n} & \\frac{\\partial p_n}{\\partial y_n}\n",
    "\\end{bmatrix}$$\n",
    "\n",
    "We will linearize $\\mathbf H$ by computing the partial for $x$ as\n",
    "\n",
    "$$ \\frac{estimated\\_x\\_position - satellite\\_x\\_position}{estimated\\_range\\_to\\_satellite}$$\n",
    "\n",
    "The equation for $y$ just substitutes $y$ for $x$.\n",
    "\n",
    "Then the algorithm is as follows.\n",
    "\n",
    "    def ILS:\n",
    "        guess position\n",
    "        while not converged:\n",
    "            compute range to satellites for current estimated position\n",
    "            compute H linearized at estimated position\n",
    "            compute new estimate delta from (H^T H)'H^T dz\n",
    "            new estimate = current estimate + estimate delta\n",
    "            check for convergence\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iteration: 1\n",
      "innovation [-148.512    28.6789 -148.5361]\n",
      "pos       [805.4175 205.2868]\n",
      "\n",
      "\n",
      "iteration: 2\n",
      "innovation [-0.1177 -7.4049 -0.1599]\n",
      "pos       [800.04   199.9746]\n",
      "\n",
      "\n",
      "iteration: 3\n",
      "innovation [-0.0463 -0.001  -0.0463]\n",
      "pos       [800. 200.]\n",
      "\n",
      "\n",
      "iteration: 4\n",
      "innovation [-0. -0. -0.]\n",
      "pos       [800. 200.]\n",
      "\n",
      "\n",
      "Iterated solution:  [800. 200.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from numpy.linalg import norm, inv\n",
    "from numpy.random import randn\n",
    "from numpy import dot\n",
    "\n",
    "np.random.seed(1234)\n",
    "user_pos = np.array([800, 200])\n",
    "\n",
    "sat_pos = np.asarray(\n",
    "    [[0, 1000],\n",
    "     [0, -1000],\n",
    "     [500, 500]], dtype=float)\n",
    "\n",
    "def satellite_range(pos, sat_pos):\n",
    "    \"\"\" Compute distance between position 'pos' and \n",
    "    the list of positions in sat_pos\"\"\"\n",
    "\n",
    "    N = len(sat_pos)\n",
    "    rng = np.zeros(N)\n",
    "\n",
    "    diff = np.asarray(pos) - sat_pos\n",
    "\n",
    "    for i in range(N):\n",
    "        rng[i] = norm(diff[i])\n",
    "\n",
    "    return norm(diff, axis=1)\n",
    "\n",
    "\n",
    "def hx_ils(pos, sat_pos, range_est):\n",
    "    \"\"\" compute measurement function where\n",
    "    pos : array_like \n",
    "        2D current estimated position. e.g. (23, 45)\n",
    "        \n",
    "    sat_pos : array_like of 2D positions\n",
    "        position of each satellite e.g. [(0,100), (100,0)]\n",
    "        \n",
    "    range_est : array_like of floats\n",
    "        range to each satellite\n",
    "    \"\"\"\n",
    "    \n",
    "    N = len(sat_pos)\n",
    "    H = np.zeros((N, 2))\n",
    "    for j in range(N):\n",
    "        H[j, 0] = (pos[0] - sat_pos[j, 0]) / range_est[j]\n",
    "        H[j, 1] = (pos[1] - sat_pos[j, 1]) / range_est[j]\n",
    "    return H\n",
    "\n",
    "\n",
    "def lop_ils(zs, sat_pos, pos_est, hx, eps=1.e-6):\n",
    "    \"\"\" iteratively estimates the solution to a set of\n",
    "    measurement, given known transmitter locations\"\"\"\n",
    "    pos = np.array(pos_est)\n",
    "\n",
    "    with book_format.numpy_precision(precision=4):\n",
    "        converged = False\n",
    "        for i in range(20):\n",
    "            r_est = satellite_range(pos, sat_pos)\n",
    "            print('iteration:', i+1)\n",
    "\n",
    "            H = hx(pos, sat_pos, r_est)        \n",
    "            Hinv = inv(dot(H.T, H)).dot(H.T)\n",
    "\n",
    "            # update position estimate\n",
    "            y = zs - r_est\n",
    "            print('innovation', y)\n",
    "\n",
    "            Hy = np.dot(Hinv, y)\n",
    "            pos = pos + Hy\n",
    "            print('pos       {}\\n\\n'.format(pos))\n",
    "\n",
    "            if max(abs(Hy)) < eps:\n",
    "                converged = True\n",
    "                break\n",
    "\n",
    "    return pos, converged\n",
    "\n",
    "# compute range to each sensor\n",
    "rz = satellite_range(user_pos, sat_pos)\n",
    "\n",
    "pos, converted = lop_ils(rz, sat_pos, (900, 90), hx=hx_ils)\n",
    "print('Iterated solution: ', pos)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So let's think about this. The first iteration is essentially performing the computation that the linear Kalman filter computes during the update step:\n",
    "\n",
    "$$\\begin{aligned}\n",
    "\\mathbf y &= \\mathbf z - \\mathbf{Hx}\\\\\n",
    "\\mathbf x &= \\mathbf x + \\mathbf{Ky}\n",
    "\\end{aligned}$$\n",
    "\n",
    "where the Kalman gain equals one. You can see that despite the very inaccurate initial guess (900, 90) the computed value for $\\mathbf x$, (805.4, 205.3), was very close to the actual value of (800, 200). However, it was not perfect. But after three iterations the ILS algorithm was able to find the exact answer. So hopefully it is clear why we use ILS instead of doing the sensor fusion with the Kalman filter - it gives a better result. Of course, we started with a very inaccurate guess; what if the guess was better?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iteration: 1\n",
      "innovation [-0.0009 -1.3868 -0.0024]\n",
      "pos       [800.0014 199.9991]\n",
      "\n",
      "\n",
      "iteration: 2\n",
      "innovation [-0.0016 -0.     -0.0016]\n",
      "pos       [800. 200.]\n",
      "\n",
      "\n",
      "iteration: 3\n",
      "innovation [-0. -0. -0.]\n",
      "pos       [800. 200.]\n",
      "\n",
      "\n",
      "Iterated solution:  [800. 200.]\n"
     ]
    }
   ],
   "source": [
    "pos, converted = lop_ils(rz, sat_pos, (801, 201), hx=hx_ils)\n",
    "print('Iterated solution: ', pos)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The first iteration produced a better estimate, but it still could be improved upon by iterating.\n",
    "\n",
    "I injected no noise in the measurement to test and display the theoretical performance of the filter. Now let's see how it performs when we inject noise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iteration: 1\n",
      "innovation [129.8823 100.461  107.5398]\n",
      "pos       [831.4474 186.1222]\n",
      "\n",
      "\n",
      "iteration: 2\n",
      "innovation [-31.6446  -7.4837 -30.7861]\n",
      "pos       [800.3284 198.8076]\n",
      "\n",
      "\n",
      "iteration: 3\n",
      "innovation [-0.6041 -0.3813  0.3569]\n",
      "pos       [799.948  198.6026]\n",
      "\n",
      "\n",
      "iteration: 4\n",
      "innovation [-0.4803  0.0004  0.4802]\n",
      "pos       [799.9476 198.6025]\n",
      "\n",
      "\n",
      "iteration: 5\n",
      "innovation [-0.4802  0.0007  0.4803]\n",
      "pos       [799.9476 198.6025]\n",
      "\n",
      "\n",
      "Iterated solution:  [799.948 198.602]\n"
     ]
    }
   ],
   "source": [
    "# add some noise\n",
    "nrz = []\n",
    "for z in rz:\n",
    "    nrz.append(z + randn())\n",
    "pos, converted = lop_ils(nrz, sat_pos, (601,198.3), hx=hx_ils)\n",
    "print('Iterated solution: ', pos)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we can see that the noise means that we no longer find the exact solution but we are still able to quickly converge onto a more accurate solution than the first iteration provides.\n",
    "\n",
    "This is far from a complete coverage of the iterated least squares algorithm, let alone methods used in GNSS to compute positions from GPS pseudoranges. You will find a number of approaches in the literature, including QR decomposition, SVD, and other techniques to solve the overdetermined system. For a nontrivial task you will have to survey the literature and perhaps design your algorithm depending on your specific sensor configuration, the amounts of noise, your accuracy requirements, and the amount of computation you can afford to do."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
